iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 18
0
自我挑戰組

軟體開發隨筆雜記--試著解決問題系列 第 18

[Python][OpenCV][AWS] Rekognition(3)label_rekognition

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20201003/20119608ZeQLxC1YpK.jpg

    def label_rekognition(self, event = None):
        client=boto3.client('rekognition')
        img = cv2.imread(self.imageFile)
        imOut = img
        img_PIL = Image.open(self.imageFile)
        width, height = img_PIL.size        
        with open(self.imageFile, 'rb') as image:
            label_response = client.detect_labels(
                                Image={'Bytes': image.read()})
        label_jsonfile = "labeloutput.json"
        with open(label_jsonfile, 'w') as fp:
            json.dump(label_response, fp)
        for label in label_response['Labels']:
            for boundingbox in label['Instances']:
                cv2.rectangle(imOut,
                              (int(width*boundingbox['BoundingBox']['Left']),
                               int(height*boundingbox['BoundingBox']['Top'])),
                              (int(width*(boundingbox['BoundingBox']['Left']+
                               boundingbox['BoundingBox']['Width'])),
                              int(height*(boundingbox['BoundingBox']['Top']+
                               boundingbox['BoundingBox']['Height']))),
                               self.color_1,
                               int(self.linesizespinbox.get()))
                cv2.putText(imOut,
                            label['Name'],  
                            (int(width*boundingbox['BoundingBox']['Left']),
                             int(height*boundingbox['BoundingBox']['Top'])),
                            self.fontcv2Var.get(),
                            int(self.fontsizespinbox.get()),
                            self.color_2,
                            int(self.linesizespinbox.get()), 
                            self.fontlinetypecv2Var.get())

        while True:
            cv2.imshow("Output", imOut)
            k = cv2.waitKey(0) & 0xFF
            # 若按下 q 鍵,則離開
            if k == 113:
                break
        cv2.destroyAllWindows()

上一篇
[Python][OpenCV][AWS] Rekognition(2)celibrities
下一篇
[Python][OpenCV][AWS] Rekognition(4)text_rekognition
系列文
軟體開發隨筆雜記--試著解決問題33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言